package com.demo.web.controller;

import com.demo.web.po.User;
import com.demo.web.services.UserServices;
import com.power.common.model.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * @author peter
 * @date 2022/2/26 19:08
 */
@RestController
@Slf4j
public class IndexController {

    @Resource
    private UserServices userServices;

    @GetMapping("foo")
    public String foo() {
        log.info("[IndexController.foo]: access");
        return "[web2]:bar";
    }

    @GetMapping("getAllUser")
    public ArrayList<User> getAllUser() {
        // 自动转换结果
/*        [
            {
                "name": "peter",
                    "age": 18
            },
            {
                "name": "andy",
                    "age": 18
            }
        ]*/
        ArrayList<User> allUsers = userServices.getAllUsers();
        log.info("getAllUser返回结果" + allUsers.toString());
        return allUsers;
    }

    @GetMapping("getAllUser2")
    /**
     * 通用格式返回
     * @Return CommonResult
     */
    public CommonResult getAllUser2() {
        // 自动转换结果
/*        {
  "success": true,
  "message": "succeed",
  "data": [
    {
      "name": "peter",
      "age": 18
    },
    {
      "name": "andy",
      "age": 18
    }
  ],
  "code": "0000",
  "timestamp": "2022-02-27 01:16:19"
}*/
        ArrayList<User> allUsers = userServices.getAllUsers();
        log.info("getAllUser返回结果" + allUsers.toString());
        return new CommonResult(true, "[web2]", allUsers);
    }
}
